# -*- coding: utf-8 -*-
"""
@Time ： 2023/5/9 0009 10:07
@Auth ： ysc
@File ：ImageBuildPage.py
@IDE ：PyCharm
"""
from selenium.webdriver.common.by import By
from Page.LoginPage import LoginPage
from selenium.webdriver.common.keys import Keys
from Lib.common_lib import LOG_INFO, LOG_ERROR


class ImageBuild(object):
    """镜像定制页面"""
    group_name = None
    PipelineName = None

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def check_info(self):
        """
        页面信息检查
        """
        try:
            # 流水线列表
            name_title = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/p')
            # 添加流水线按钮，用户不登陆时不显示
            pipeline_button = self.driver.find_element(By.XPATH,
                                                       '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[1]/button')
            if pipeline_button.is_displayed():
                button_info = '添加流水线按钮显示正常'
            else:
                button_info = '用户未登录，添加流水线按钮不显示'

            # 显示输入框
            filter_box = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div['
                                                            '1]/div/div/div[2]')
            if filter_box.is_displayed():
                filter_info = '显示输入框'
            else:
                filter_info = '输入框不显示'

            select_box = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div['
                                                            '1]/div/div/div[1]/div/div/div/div')
            if select_box.is_displayed():
                box_info = '显示筛选下拉框'
            else:
                box_info = '筛选下拉框不显示'

            return [name_title.text, button_info, filter_info, box_info]

        except Exception as e:
            message = '流水线列表信息元素定位失败'
            LOG_ERROR("error: found page_info timeout")
            print(e)
            LOG_INFO(message)

    def user_login(self, user_name, password):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.user_login()
        login.login_valid(user_name, password)

    def add_pipeline(self):
        """
        添加流水线按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[1]/button').click()
        except Exception as e:
            message = '添加流水线按钮元素定位失败'
            LOG_ERROR("error: found add_pipeline timeout")
            print(e)
            LOG_INFO(message)

    def div_cancel(self):
        """
        弹窗取消按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]/div/div/footer/div/button[2]'
                                               '/span').click()
        except Exception as e:
            message = '弹窗取消按钮元素定位失败'
            LOG_ERROR("error: found div_cancel timeout")
            print(e)
            LOG_INFO(message)

    def div_close(self):
        """
        弹窗关闭按钮
        """
        try:
            self.driver.find_element(By.XPATH,
                                     '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/header/button').click()
        except Exception as e:
            message = '弹窗元素关闭按钮定位失败'
            LOG_ERROR("error: found div_close timeout")
            print(e)
            LOG_INFO(message)

    def div_confirm(self):
        """
        弹窗确认按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]/div/div/footer/div/button[1]'
                                               '/span').click()
        except Exception as e:
            message = '弹窗取消按钮元素定位失败'
            LOG_ERROR("error: found div_confirm timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_check(self):
        """
        添加流水线弹窗检查
        """
        try:
            div = self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]')
            if div.is_displayed():
                div_info = '添加流水线弹窗显示正常'
            else:
                div_info = '流水线弹窗显示不存在'

            return div_info

        except Exception as e:
            message = '弹窗元素定位失败'
            LOG_ERROR("error: found pipeline_check timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_info_check(self):
        """
        添加流水线弹窗页面信息检查
        """
        try:
            info_1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[1]/label')

            info_2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[2]/label')

            info_3 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[3]/label')

            info_4 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[4]/label')

            info_5 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[5]/label')

            return [info_1.text, info_2.text, info_3.text, info_4.text, info_5.text]

        except Exception as e:
            message = '弹窗信息按钮元素定位失败'
            LOG_ERROR("error: found pipeline_info_check timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_info_valid(self):
        """
        添加流水线弹窗必填校验
        """
        try:
            info_1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[1]/div/div[2]')

            info_2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[2]/div/div[2]')

            info_3 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[3]/div/div[2]')

            info_4 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[4]/div/div[2]')

            info_5 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                        '2]/div/div/div/form/div[5]/div/div[2]')

            return [info_1.text, info_2.text, info_3.text, info_4.text, info_5.text]

        except Exception as e:
            message = '弹窗校验信息元素定位失败'
            LOG_ERROR("error: found pipeline_info_valid timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_type(self):
        """
        流水线类型
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                               '1]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线类型下拉框定位失败'
            LOG_ERROR("error: found pipeline_type timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_group(self):
        """
        流水线分组
        """
        try:
            self.group_name = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                                 '2]/div/div/div/form/div[last() - '
                                                                 '3]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线分组输入框定位失败'
            LOG_ERROR("error: found pipeline_group timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_name(self):
        """
        流水线名称
        """
        try:
            self.PipelineName = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                                   '2]/div/div/div/form/div[last() - 2]/div/div['
                                                                   '1]/div/input')
        except Exception as e:
            message = '添加流水线名称输入框定位失败'
            LOG_ERROR("error: found pipeline_name timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_format(self):
        """
        流水线格式
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div[last('
                                               ') - 1]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_format timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_arch(self):
        """
        流水线架构
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div[last('
                                               ')]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_arch timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_scene(self):
        """
        流水线场景-仅镜像定制
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                               '2]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_scene timeout")
            print(e)
            LOG_INFO(message)

    def switch_pipeline_type(self, build_type="vm"):
        """
        选择镜像类型
        """
        if build_type == "vm":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = '版本镜像定位失败'
                LOG_ERROR("error: found switch_pipeline_version_image timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = '镜像定制定位失败'
                LOG_ERROR("error: found switch_pipeline_image_build timeout")
                print(e)
                LOG_INFO(message)

    def switch_pipeline_format(self, build_format="iso"):
        """
        选择镜像格式
        """
        if build_format == "iso":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = 'iso格式定位失败'
                LOG_ERROR("error: found switch_pipeline_iso timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "docker":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = 'docker格式定位失败'
                LOG_ERROR("error: found switch_pipeline_docker timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[3]').click()
            except Exception as e:
                message = 'stratovirt格式定位失败'
                LOG_ERROR("error: found switch_pipeline_stratovirt timeout")
                print(e)
                LOG_INFO(message)

    def switch_pipeline_arch(self, build_arch="arm"):
        """
        选择镜像架构
        """
        if build_arch == "arm":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = 'arm格式定位失败'
                LOG_ERROR("error: found switch_pipeline_arm timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = 'x86格式定位失败'
                LOG_ERROR("error: found switch_pipeline_x86 timeout")
                print(e)
                LOG_INFO(message)

    def switch_pipeline_scene(self, build_scene="service"):
        """
        选择镜像场景
        """
        if build_scene == "service":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = '服务器镜像场景定位失败'
                LOG_ERROR("error: found switch_pipeline_service timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = '嵌入式镜像场景定位失败'
                LOG_ERROR("error: found switch_pipeline_embedding timeout")
                print(e)
                LOG_INFO(message)

    def pipeline_length_valid(self, group_name, pipeline_name):
        """
        添加流水线长度校验
        """
        # 输入流水线分组名
        self.group_name.send_keys(group_name)
        self.group_name.send_keys(Keys.ENTER)
        # 输入流水线名称
        self.PipelineName.send_keys(pipeline_name)

    def length_info_valid(self):
        """
        获取长度校验提示信息
        """
        warn_info_1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                         '2]/div/div/div/form/div[2]/div/div[2]')

        warn_info_2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                         '2]/div/div/div/form/div[3]/div/div[2]')

        return [warn_info_1.text, warn_info_2.text]